package com.lvtu.monitor.storm.entity;

import java.io.Serializable;
import java.util.Map;

import com.alibaba.fastjson.annotation.JSONField;

/**
 * @Title: NginxLog.java
 * @Package com.lvtu.monitor.storm.entity
 * @Description: 对应httpsqs队列中取出的nginx日志
 * @author lvzimin
 * @date 2015年5月19日 上午11:09:44
 * @version V1.0.0
 */
public class NginxLog implements Serializable {

	private static final long serialVersionUID = -7506569473260140787L;

	/**
	 * 客户端IP地址(remoteAddr)
	 */
	private String ip;

	/**
	 * 通用日志格式下的本地时间(timeLocal)
	 */
	private long time;

	/**
	 * 客户端请求的域名(httpHost)
	 */
	private String host;

	/**
	 * 状态码
	 */
	private int status;

	/**
	 * 发送给客户端的总字节数(bodyBytesSent)
	 */
	private int bytes;

	/**
	 * http请求端的真正IP(httpXforwardedFor)
	 */
	private String xfor;

	/**
	 * 客户端浏览器相关信息(httpUserAgent)
	 */
	private String ua;

	/**
	 * 请求响应时间(单位:秒)(requestTime)
	 */
	private float cost;

	/**
	 * http方法(GET,POST)(httpMethod)
	 */
	private String hm;

	/**
	 * 请求uri(requestUri)
	 */
	private String uri;

	/**
	 * 请求url(包含参数)
	 */
	private String url;

	/**
	 * 请求参数
	 */
	@JSONField(serialize = false)
	private Map<String, Object> params;

	public String getIp() {
		return ip;
	}

	public void setIp(String ip) {
		this.ip = ip;
	}

	public long getTime() {
		return time;
	}

	public void setTime(long time) {
		this.time = time;
	}

	public String getHost() {
		return host;
	}

	public void setHost(String host) {
		this.host = host;
	}

	public int getStatus() {
		return status;
	}

	public void setStatus(int status) {
		this.status = status;
	}

	public int getBytes() {
		return bytes;
	}

	public void setBytes(int bytes) {
		this.bytes = bytes;
	}

	public String getXfor() {
		return xfor;
	}

	public void setXfor(String xfor) {
		this.xfor = xfor;
	}

	public String getUa() {
		return ua;
	}

	public void setUa(String ua) {
		this.ua = ua;
	}

	public float getCost() {
		return cost;
	}

	public void setCost(float cost) {
		this.cost = cost;
	}

	public String getHm() {
		return hm;
	}

	public void setHm(String hm) {
		this.hm = hm;
	}

	public String getUri() {
		return uri;
	}

	public void setUri(String uri) {
		this.uri = uri;
	}

	public void setParams(Map<String, Object> params) {
		this.params = params;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	/** 取出RopRequest中的公共参数 **/

	/**
	 * 接口方法
	 * 
	 * @return
	 */
	public String getMethod() {
		if (isHessianRequest()) {
			return this.uri;
		} else if (this.params != null) {
			String method = String.valueOf(this.params.get("method"));
			if (method.contains(",")) {
				method = method.substring(0, method.indexOf(","));
			}
			return method;
		}
		return null;
	}

	/**
	 * 接口版本
	 * 
	 * @return
	 */
	public String getVersion() {
		if (isHessianRequest()) {
			return null;
		} else if (this.params != null) {
			String version = String.valueOf(this.params.get("version"));
			if (version.contains(",")) {
				version = version.substring(0, version.indexOf(","));
			}
			return version;
		}
		return null;
	}

	/**
	 * 客户端token
	 * 
	 * @return
	 */
	public String getToken() {
		if (this.params != null) {
			return String.valueOf(this.params.get("token"));
		}
		return null;
	}

	/**
	 * lvsessionId
	 * 
	 * @return
	 */
	public String getLvsessionid() {
		if (this.params != null) {
			return String.valueOf(this.params.get("lvsessionid"));
		}
		return null;
	}

	/**
	 * 客户端设备号
	 * 
	 * @return
	 */
	public String getUdid() {
		if (this.params != null) {
			return String.valueOf(this.params.get("udid"));
		}
		return null;
	}

	/**
	 * 客户端操作系统版本
	 * 
	 * @return
	 */
	public String getOsVersion() {
		if (this.params != null) {
			return String.valueOf(this.params.get("token"));
		}
		return null;
	}

	/**
	 * 设备名称
	 * 
	 * @return
	 */
	public String getDeviceName() {
		if (this.params != null) {
			return String.valueOf(this.params.get("deviceName"));
		}
		return null;
	}

	/**
	 * 第一渠道
	 * 
	 * @return
	 */
	public String getFirstChannel() {
		if (this.params != null) {
			return String.valueOf(this.params.get("firstChannel"));
		}
		return null;
	}

	/**
	 * 第二渠道
	 * 
	 * @return
	 */
	public String getSecondChannel() {
		if (this.params != null) {
			return String.valueOf(this.params.get("secondChannel"));
		}
		return null;
	}

	/**
	 * app版本号
	 * 
	 * @return
	 */
	public String getLvversion() {
		if (this.params != null) {
			return String.valueOf(this.params.get("lvversion"));
		}
		return null;
	}

	/**
	 * 设备纬度
	 * 
	 * @return
	 */
	public String getLatitude() {
		if (this.params != null) {
			return String.valueOf(this.params.get("latitude"));
		}
		return null;
	}

	/**
	 * 设备经度
	 * 
	 * @return
	 */
	public String getLongitude() {
		if (this.params != null) {
			return String.valueOf(this.params.get("longitude"));
		}
		return null;
	}

	/**
	 * 是否是hessian请求
	 * 
	 * @return
	 */
	private boolean isHessianRequest() {
		return !ua.isEmpty() && ua.startsWith("Java");
	}

	@Override
	public String toString() {
		return "NginxLog [ip=" + ip + ", time=" + time + ", host=" + host
				+ ", status=" + status + ", bytes=" + bytes + ", xfor=" + xfor
				+ ", ua=" + ua + ", cost=" + cost + ", hm=" + hm + ", uri="
				+ uri + ", url=" + url + ", params=" + params + "]";
	}

}
